<?php

namespace app\admin\controller\teacher;

use app\common\controller\Backend;

class Subject extends Backend
{
    protected $model = null;

    protected $relationSearch = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Subject.Subject');
        $this->CategoryModel = model('Subject.Category');
        $this->TeacherModel = model('Subject.Teacher.Teacher');
        // 查询某一列的值并且指定索引
        $cate = $this->CategoryModel->select();
        $teacher = $this->TeacherModel->select();
        $catelist = [];
        $teacherlist = [];
        foreach ($cate as $item)
        {
            $catelist[$item['id']] =  $item['name'];
        }
        foreach ($teacher as $item)
        {
            $teacherlist[$item['id']] =  $item['name'];
        }
        $this->view->assign([
            'catelist' => $catelist,
            'teacherlist' => $teacherlist
        ]);
    }

    // 查看
    public function index()
    {
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            // 加入教师查询条件
            $where['teacherid'] = $ids;

            $total = $this->model
                ->with(['category'])
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with(['category'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
}
